package com.hydl.power.service.impl;

import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.ProjectMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.Project;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.ProjectService;
import com.hydl.power.utils.ChineseInital;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 根据城市Id查询城市下项目
    * */
    @Override
    public List<Project> queryProjectId(String cityId) {
        return projectMapper.queryProjectId(cityId);
    }

    /**
     * 查询所有项目
     * */
    @Override
    public List<Project> queryProjectAll(String token) {
        SysUser sysUser = sysUserMapper.getUserByToken(token);
        if(sysUser.getStatus() == 2 ){
            List<Project> project=projectMapper.queryByToken(token);
            return project;
        }
        return projectMapper.queryProjectAll();
    }


    /**
     * 根据Id删除项目
     * */
    @Override
    public int deleteProjectById(Integer id) {
        return projectMapper.deleteProjectById(id);
    }
    /**
     * 添加项目
     * */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addProject(String cityId, String projectName, String longitudeLatitude,String latitudeLatitude,String createTableStatus,String createTemperatureDataTableStatus) {
        String powerDataTableName=null;
        String powerDataFlag=null;
        if ("1".equals(createTableStatus)){
            String str = ChineseInital.getAllFirstLetter(projectName);
            powerDataTableName="powerdata_"+str+"_"+new Random(1).nextInt(100);
            projectMapper.createProjectPowerDataTable(powerDataTableName);
        }
        String temperatureDataTableName=null;
        if ("1".equals(createTemperatureDataTableStatus)){
            String str = ChineseInital.getAllFirstLetter(projectName);
            temperatureDataTableName="temperature_data_"+str+"_"+new Random(1).nextInt(100);

            projectMapper.createTemperatureDataTable(temperatureDataTableName);
        }
      if("1".equals(createTableStatus) && "1".equals(createTemperatureDataTableStatus)){
        powerDataFlag="0";
      }else if("1".equals(createTableStatus)){
        powerDataFlag="1";
      }
      else{
        powerDataFlag="2";
      }

        return projectMapper.addProject(cityId,projectName,longitudeLatitude,latitudeLatitude,powerDataTableName,temperatureDataTableName,powerDataFlag);
    }

    /**
     * 修改项目
     * */
    @Override
    public int updateProject( String projectName,Integer id) {
        return projectMapper.updateProject(projectName,id);
    }

    /**
     * 根据项目Id查询信息
     * */
    @Override
    public Project queryProjectByProId(String projectId) {
        return projectMapper.queryProjectByProId(projectId);
    }

    @Override
    public Project queryProjectById(Integer id) {
        return projectMapper.queryProjectById(id);
    }
    /*
    * 根据城市Id、token查询城市下项目
    * */
    @Override
    public List<Project> queryProjectByToken(String cityId,String token) {
        return projectMapper.queryProjectByToken(cityId,token);
    }
    /*
     * 模糊查询
     * */
    @Override
    public PageInfo<Project> queryProjectLike(Integer pageNum, Integer pageSize, String text, String token) {
        SysUser sysUser =sysUserMapper.getUserByToken(token);
        if(sysUser!=null){
            if(sysUser.getStatus()==2) {
                return new PageInfo<>(projectMapper.queryProjectLike(pageNum,pageSize,text,sysUser.getProjectId().toString()));
            }
        }
        return new PageInfo<>(projectMapper.queryProjectLike(pageNum,pageSize,text,null));
    }

    @Override
    public int selectCountByCityId(String cityId) {
        return projectMapper.selectCountByCityId(cityId);
    }
}
